package com.interceptor;

import io.grpc.*;
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall;
import io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener;

public class HeaderClientInterceptor implements ClientInterceptor {

    @Override
    public ClientCall interceptCall(MethodDescriptor method, CallOptions callOptions, Channel next) {
        return new SimpleForwardingClientCall(next.newCall(method, callOptions)) {

            @Override
            public void start(Listener respTListener, Metadata headers) {
                if (headers != null) {
                    Metadata fixedHeaders = new Metadata();
                    Metadata.Key<String> key = Metadata.Key.of("jobid", Metadata.ASCII_STRING_MARSHALLER);
//                    fixedHeaders.put(key, "job_id_1");
                    fixedHeaders.put(key, "job_id_2");
                    headers.merge(fixedHeaders);
                }
                super.start(new SimpleForwardingClientCallListener(respTListener) {
                    @Override
                    public void onHeaders(Metadata headers) {
                        super.onHeaders(headers);
                    }
                }, headers);
            }
        };
    }
}